package com.kaka.bus.engine.home.info.changepwd;


import android.content.Intent;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.kaka.bus.AppCenter;
import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.user.login.LoginActivity;
import com.kaka.bus.util.U;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;

/**
 * Created by zhangqi on 2016/11/15.
 */

public class ChangeActivity extends BaseActivity<ChangePresenter> implements ChangeContract.View {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_change_jiu)
    EditText mEdtChangeJiu;
    @BindView(R.id.edt_change_new)
    EditText mEdtChangeNew;
    @BindView(R.id.edt_change_newagain)
    EditText mEdtChangeNewagain;
    @BindView(R.id.btn_change_next)
    AppCompatButton mBtnChangeNext;

    @Override
    protected void initView() {
        super.initView();
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("修改密码");
//        SpannableString ss = new SpannableString("请输入6位数字或字母及组合作为登录密码");//定义hint的值
//        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(12, true);//设置字体大小 true表示单位是sp
//        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        mEdtChangeNew.setHint(new SpannedString(ss));
    }

    @Override
    protected void initListener() {
        RxView.clicks(mBtnChangeNext).throttleFirst(3, TimeUnit.SECONDS)
                .subscribe(v -> {
                    mPresenter.changePwd(getUserId(), getToken(), getText(mEdtChangeJiu), getText(mEdtChangeNew), getText(mEdtChangeNewagain), AppCenter.getTokenKey());
                });
    }

    @Override
    public void onSuccess(HttpWrapper<String> wrapper) {
        if (Constant.TOKEN_TIMEOUT.equals(wrapper.code)) {
            U.tokenOut(this);
        } else if (Constant.SUCCESS_CODE.equals(wrapper.code)) {
            showMessage("密码修改成功,请重新登录!");
            setUserInfo(null);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            AppCenter.cloaseApp();
        } else {
            showMessage(wrapper.info);
        }
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    @Override
    public void paramsFailed(String msg) {
        showMessage(msg);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_change;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

}
